﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using FireStarter.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test.Logic;
using FireStarterModeller.UI;
using FireStarterModeller.UI.ViewEditor;
using Habanero.Base;
using Habanero.BO.ClassDefinition;
using Habanero.Faces.Base;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.ViewEditor
{
    [TestFixture]
    public class TestUIPreviewControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        private static IControlFactory GetControlFactory()
        {
            return GlobalUIRegistry.ControlFactory;
        }

        [Test]
        public void Test_PreviewGrid()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty dmProperty = dmClass.Properties[0];

            DMToEntityClassDefsConverter converter = new DMToEntityClassDefsConverter();
            ClassDefCol col = converter.Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = col["FireStarterModeller.BO", "Entity_" + dmClass.ClassNameBO];
            IUIPreviewControl control = new UIPreviewControl(GetControlFactory());

            UIGrid grid = new UIGrid();
            grid.Add(new UIGridColumn("Column1", dmProperty.PropertyName, typeof(DataGridViewTextBoxColumn), false, 100, PropAlignment.left, new Hashtable()));

            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            control.PreviewGrid(classDef, grid);
            
            //---------------Test Result -----------------------
            Assert.IsNotNull(control.GridControl);
            Assert.AreEqual(2, control.GridControl.Grid.Columns.Count);
       
        }


        [Test]
        public void Test_PreviewForm()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty dmProperty = dmClass.Properties[0];

            DMToEntityClassDefsConverter converter = new DMToEntityClassDefsConverter();
            ClassDefCol col = converter.Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = col["FireStarterModeller.BO", "Entity_" + dmClass.ClassNameBO];
            IUIPreviewControl control = new UIPreviewControl(GetControlFactory());
            UIForm form = new UIForm();
            UIFormTab tab = new UIFormTab();
            UIFormColumn column = new UIFormColumn();
            column.Add(new UIFormField("Field1", dmProperty.PropertyName, typeof(TextBox), "TextBoxMapper", BOBroker.GetMapperAssemblyName(), true, "", new Hashtable(), LayoutStyle.Label));
            tab.Add(column);
            form.Add(tab);
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            control.PreviewForm(classDef, form);
            //---------------Test Result -----------------------

            Assert.IsNotNull(control.FormPanelInfo);
            Assert.AreEqual(1, control.FormPanelInfo.FieldInfos.Count);
            Assert.AreEqual(dmProperty.PropertyName, control.FormPanelInfo.FieldInfos[0].PropertyName);
            Assert.IsTrue(control.FormPanelInfo.FieldInfos[0].InputControl.Enabled);      
          }
    }

}
